package www.lagou.Dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import www.lagou.entity.Dept;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;

public class DeptDao {
    /**
     * 根据部门ID查询部门的名称
     */
    public Dept getDept(int id) throws SQLException {
        if(0 == id){    //判断部门字段的值为空，则强调声明该员工未加入部门
            Dept dept = new Dept();
            dept.setDeptName("未加入部门");
            return dept;
        }
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());//设置自动创建连接模式
        //创建sql语句查询指定id的部门名称
        String sql = "select deptName from Dept where id = ?;";
        //传入参数id然后执行查询命令，返回Bean类封装的结果集对象
        Dept dept = qr.query(sql,new BeanHandler<Dept>(Dept.class),id);
        return dept;
    }
}
